<?php
/**
 * 权限管理之节点管理
 * Date: 2015/6/18
 * Time: 15:44
 */

namespace AdminV3\Controller;
use Think\Controller;
class NodeController extends CommonController
{
    //节点列表
    public function index()
    {
        $list =array();
        $n =0 ;
        $moduleList = M('node')->where('level =1')->select();
        //var_dump($moduleList);
        foreach($moduleList as $k=>$v){
            $list[$n]=$v;
            $n++;
            $controllerList = M('node')->where('level =2 and pid ='.$v['id'])->select();
                foreach($controllerList as $ka=>$va){
                    $va['t'] ='┊┄┣━━ ';
                    $list[$n]=$va;
                    $n++;
                    $actionList = M('node')->where('level = 3 and pid='.$va['id'])->select();
                    foreach($actionList as $key=>$val){
                        $val['t'] = '┊┄┄┄┄┄┣━━ ';
                        $list[$n]=$val;
                        $n++;
                    }
                }
        }

        $this->assign('list',$list);
        $this->display();
    }

    //添加节点
    public function addNode()
    {
        if(isset($_GET['nid'])){
            $node = M('Node')->find($_GET['nid']);
            //var_dump($node);
            $nodeList = M('node')->where('level !=3 and id !='.I('get.nid'))->group('level,sort')->select();
            //var_dump($nodeList);
            $this->assign('node',$node);
        }else{
            $nodeList = array();
            $n = 0;

            $nodeLevel1 = M('node')->where('level=1')->select();

            foreach($nodeLevel1 as $val){
                $n ++;
                $nodeList[$n] =$val;
                $nodeLevel2 = M('node')->where('level =2 and pid ='.$val['id'])->group('sort')->select();

                foreach($nodeLevel2 as $value){
                    $n++;
                    $nodeList[$n] = $value;
                }

            }
            //var_dump($nodeList);
        }
        $this->assign('nodeList',$nodeList);
        //var_dump($nodeList);
        $this->display();
    }

    //存入数据库
    public function insertNode()
    {
        if(isset($_POST['name']) && isset($_POST['title']) && isset($_POST['pid'])
            && isset($_POST['level']) && isset($_POST['status']) && isset($_POST['sort']) ){
            $db= M('Node');
            if($db->create()){
                if($db->add()){
                    $this->redirect('Node/index');
                }else{
                    $this->error('添加节点失败！');
                }
            }else{
                $this->error('数据不完整！请重新填写');
            }
        }else{
            $this->error('数据不完整！请重新填写');
        }

    }

    //更新节点信息
    public function saveNode()
    {
        if(isset($_POST['nid'])){
            if(isset($_POST['name']) && isset($_POST['title']) && isset($_POST['pid'])
                && isset($_POST['level']) && isset($_POST['status']) && isset($_POST['sort']) ){
                $db= M('Node');
                if($db->create()){
                    if($db->where('id='.I('post.nid'))->save() === false){
                        $this->error('修改节点失败！');
                    }else{
                        $this->redirect('Node/index');
                    }
                }else{
                    $this->error('数据不完整！请重新填写');
                }
            }else{
                $this->error('数据不完整！请重新填写');
            }
        }else{
            $this->error('非法操作，请按正常流程来操作！');
        }

    }

    //改变status
    //0 为开启 1 为禁止
    public function es()
    {
        if(isset($_GET['nid']) && isset($_GET['st'])){

            $status = $_GET['st'] == 1?0:1;
            $db = M('node')->where('id='.I('get.nid'))->setField('status',$status);
            if($db === false){
                $this->error('状态修改失败');
            }else{
                $this->redirect('Node/index');
            }
        }else{
            $this->error('非法操作，请按正常流程来操作！');
        }

    }


}
